A variable refers to a storage location in the computer’s memory that one can set aside to save, retrieve, and manipulate data.
To initialize a variable in C++, you must first declare it's data type (will be discussed next), a name for the variable
and assigned a value with the assignment operator
int var = 10; // data_type name = value;
Variables can also be declared uninitialized, this means it doesn't have a value yet, and this is done by not adding an assignment value.
In C++, there are 4 primitive data types, these are integers (
int), double floating point (
char), and boolean (
bool). As well as a common data type used along the primitive type is strings(
The integer data type is a- non-decimal number that can be positive or negative. An integer variable
is declared with the
int keyword, and keep in mind it can not be manipulated along the double data type.
An integer typically requires 4 bytes of memory space and ranges from -2³¹ to 2³¹.
int foo = 89
The double data type is a decimal point number requiring 8 bytes of memory, and is declared using
double foo = 0.78
The character data type is a single character that is wrapped within single quotes
' '. They typically
require 1 byte of memory, and is declared by the
char letter = 'A'
Strings are an array of characters and are wrapped within double quotes
" ". To declare a string
you will need to use
std::string word = "hello";
A boolean data type is a value that is either
false, and is declared using the
bool condition = true
C++ supports different types of arithmetic operators that can perform common mathematical operations:
%modulo (yields the remainder)
std::cin which stands for “character input”, reads user input from the keyboard.
Here, the user can enter a number, press
enter, and that number will get stored in
int tip = 0; std::cout << "Enter amount: "; std::cin >> tip;